package com.byx.chatserver.mapper;

import com.byx.chatserver.entity.Friend;
import com.byx.chatserver.entity.SimpleFriend;
import com.byx.chatserver.vo.FriendParam;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * IFriendMapper
 *
 * @author Superb
 * @date 2021/4/30 22:10
 */
@Mapper
public interface IFriendMapper {

    @Select("select min(fg_id)from friend_groups where fg_user_id = #{userId}")
    Integer queryUserDefaultGroupId(Long userId);

    /**
     * 根据用户id查询其所有好友
     * @param id 用户id
     * @return 用户列表
     */
    @Select("SELECT\n" +
            "\tu_id as id,\n" +
            "\tf_friend_id as friendLoginId,\n" +
            "\tf_remark_name as friendRemarkName,\n" +
            "\tu_nick_name as friendNickName,\n" +
            "\tu_avatar as friendAvatar,\n" +
            "\tfg_name as groupName\n" +
            "\tFROM\n" +
            "\tfriends INNER JOIN `user` ON f_friend_id = u_id\n" +
            "\tLEFT JOIN `friend_groups` ON f_friend_groups_id = fg_id \n" +
            "WHERE\n" +
            "\tf_user_id = #{id}")
    List<SimpleFriend> getAllFriends(Long id);

    @Select("SELECT\n" +
            "\tf_friend_id as id,\n" +
            "\tu_login_id as friendLoginId,\n" +
            "\tu_nick_name as friendNickName,\n" +
            "\tu_sex as sex,\n" +
            "\tu_age as age,\n" +
            "\tu_description as description,\n" +
            "\tu_birthday as birthday,\n" +
            "\tu_telephone as telephone,\n" +
            "\tu_real_name as realName,\n" +
            "\tu_email as email,\n" +
            "\tu_avatar as friendAvatar,\n" +
            "\tu_vocation as vocation,\n" +
            "\tf_remark_name as friendRemarkName,\n" +
            "\tfg_name as groupName\n" +
            "FROM\n" +
            "\t`user`\n" +
            "\tINNER JOIN friends ON f_friend_id = u_id\n" +
            "\tLEFT JOIN friend_groups ON f_friend_groups_id = fg_id \n" +
            "WHERE\n" +
            "\tf_user_id = #{user}\n" +
            "\tAND f_friend_id = #{friend}")
//    @Results(id="friendMap",value= {
//            @Result(column = "id", property = "id"),
//            @Result(column = "friendLoginId", property = "friendLoginId"),
//            @Result(column = "friendNickName", property = "friendNickName"),
//            @Result(column = "sex", property = "sex"),
//            @Result(column = "age", property = "age"),
//            @Result(column = "description", property = "description"),
//            @Result(column = "birthday", property = "birthday"),
//            @Result(column = "telephone", property = "telephone"),
//            @Result(column = "realName", property = "realName"),
//            @Result(column = "email", property = "email"),
//            @Result(column = "avatar", property = "avatar"),
//            @Result(column = "vocation", property = "vocation"),
//            @Result(column = "friendRemarkName", property = "friendRemarkName"),
//            @Result(column = "groupName", property = "groupName")
//    })
    Friend getFriend(@Param("user") Long user, @Param("friend") Long friend);

    @Select("SELECT\n" +
            "\tf_friend_id as id,\n" +
            "\tu_login_id as friendLoginId,\n" +
            "\tu_nick_name as friendNickName,\n" +
            "\tu_sex as sex,\n" +
            "\tu_age as age,\n" +
            "\tu_description as description,\n" +
            "\tu_birthday as birthday,\n" +
            "\tu_telephone as telephone,\n" +
            "\tu_real_name as realName,\n" +
            "\tu_email as email,\n" +
            "\tu_avatar as friendAvatar,\n" +
            "\tu_vocation as vocation,\n" +
            "\tf_remark_name as friendRemarkName,\n" +
            "\tfg_name as groupName\n" +
            "FROM\n" +
            "\t`user`\n" +
            "\tINNER JOIN friends ON f_friend_id = u_id\n" +
            "\tLEFT JOIN friend_groups ON f_friend_groups_id = fg_id \n" +
            "WHERE\n" +
            "\tf_user_id = #{user}\n" +
            "\tAND u_login_id = #{friend}")
    Friend getFriendByLoginId(@Param("user") Long user, @Param("friend") String friendLoginId);

    /**
     * 向关系表插入一条用户-好友记录
     * @param userId 用户id
     * @param friendId 好友id
     */
    @Insert("insert into user_friends_relationship(user_id,user_friend_id) values (#{userId},#{friendId})")
    void insertFriend(int userId,int friendId);

    @Insert("insert into " +
            "friends(f_user_id, f_friend_id, f_remark_name, f_friend_groups_id) " +
            "values (#{userId},#{friendId},#{remark},#{friendGroupId})")
    void insertFriendByParam(FriendParam friendParam);

    @Delete("delete from friends where f_user_id=#{userId} and f_friend_id=#{friendId}")
    void deleteFriendById(Long userId, Long friendId);


    @Update("update friends " +
            "set f_remark_name = #{remark} " +
            "where f_user_id = #{userId} and f_friend_id = #{friendId}")
    void changeFriendRemarkName(Long userId,Long friendId,String remark);

}
